package com.shelpe.services.sinbad.controller;

import java.util.ArrayList;
import java.util.List;

import com.shelpe.services.sinbad.controller.InvokesChunk.Lengthable;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class InvokesChunk<T extends Lengthable> {

	private InvokesChunk<T> next;
	private int limit = 100;
	private int maxLenth = 1500;
	
	private List<T> words;
	
	private int totalLength = 0;
	
	public InvokesChunk(){
		
	}
	
	public InvokesChunk(int limit, int max){
		this();
		this.limit = limit;
		this.maxLenth = max;
	}
	
	public InvokesChunk<T> add(T word){
		if(words == null){
			this.words = new ArrayList<T>();
		}
		if(this.words.size() < limit && (this.maxLenth == -1 || this.totalLength < this.maxLenth)){
			this.words.add(word);
			totalLength = totalLength + word.length();
		}else{
			if(next == null){
				InvokesChunk<T> newChunk = new InvokesChunk<T>(this.limit, this.maxLenth);
				next = newChunk;
			}
			next.add(word);
		}
		return this;
	}
	
	public static interface ChunkCallback<I extends Lengthable, O>{
		void call(List<I> inputs, List<O> results);
	}
	
	public static interface Lengthable{
		int length();
	}

}
